const os = require('os');
const path = require('path');
const webpack = require('webpack');
//提取样式插件，可以将你的样式提取到单独的css文件里
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
//生成HTML的插件， 具体可以去这里查看https://www.npmjs.com/package/html-webpack-plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 获取当前环境
const devEnv = process.env.NODE_ENV === 'development';
// 显示打包进度
const SimpleProgress = require('simple-progress-webpack-plugin');
// dist路径
global.dist = path.join(__dirname, '.', 'dist');
// 版本号：时间戳
global.now = Math.floor(Date.now() / 1000).toString();

// webpack 编译配置
let getBableLoader = (prod) => {
    var options = [{
        loader: 'babel-loader',
        options: {
            cacheDirectory: true
        }
    }];
    return options;
},
    plugins = [
        new webpack.HashedModuleIdsPlugin({
            hashDigestLength: 8
        }),

        // 拆分css
        new MiniCssExtractPlugin({
            filename: "css/[name].css?v=" + global.now,
            chunkFilename: "css/[id].[hash:5].chunk.css"
        }),

        // DllReferencePlugin
        new webpack.DllReferencePlugin({
            context: path.resolve(__dirname, 'dist', 'js'),
            manifest:require("./common-manifest.json")
        }),

        new HtmlWebpackPlugin({
            title: 'Phaser开发',
            filename: 'html/index.html',
            template: './src/temp/index.html',
            domain:devEnv?"project.com":'wuliaoqing.gitee.io',
            assets:devEnv?"phaser/game-shoot-ship/dist/js":'ship/dist/js',
            inject: true,
            catch: true,
            chunks: 'index',
            chunksSortMode: 'none'
        }),
        // 显示构建进度
        new SimpleProgress({
            format: 'compact'
        })
    ];

let CommonConfig = {
    // 生成 source map
    devtool: 'source-map',

    // 抛出错误
    bail: true,

    // 缓存
    cache: true,

    // 模式
    mode: devEnv ? 'development' : 'production',

    // 性能
    performance: {
        // 枚举
        hints: "warning",
        // 整数类型（以字节为单位）,300Kb
        maxAssetSize: 300000,
        // 整数类型（以字节为单位）,400Kb
        maxEntrypointSize: 400000,
        // 过滤检测文件
        assetFilter: function (assetFilename) {
            // 提供资源文件名的断言函数
            return /\.(css|js)$/.test(assetFilename);
        }
    },

    // 入口
    entry: {
        index: [
            // 'core-js/es6/map',
            // 'core-js/es6/set',
            // 'core-js/es6/promise',
            './src/entry/index.js'
        ]
    },

    // 出口
    output: {
        path: path.resolve(global.dist),
        publicPath: global.cdn + "/dist/",
        filename: 'js/[name].js?v=' + global.now,
        chunkFilename: 'js/[name].[chunkhash:5].chunk.js'
    },

    // 解析(Resolve)
    resolve: {
        extensions: ['.js', '.jsx', '.less', '.css', '*'],
        alias: {
            'game':path.join(__dirname, '.', 'game'),
            'assets':path.join(__dirname, '.','assets')
        }
    },

    // 模块(Module)
    module: {
        rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules|plugins/,
            use: getBableLoader(!devEnv)
        }, {
            test: /\.css$/,
            use: [{
                loader: MiniCssExtractPlugin.loader,
                options: {
                    hmr: global.config === 'server',
                    reloadAll: true,
                }
            },
                'css-loader',
                'postcss-loader'
            ]
        }, {
            test: /\.less$/,
            use: [{
                loader: MiniCssExtractPlugin.loader,
                options: {
                    hmr: global.config === 'server',
                    reloadAll: true,
                }
            },
                'css-loader',
                'postcss-loader',
                'less-loader'
            ]
        },  {
            test: /\.(png|jpg|gif)/,
            use: [{
                loader: 'file-loader',
                options: {
                    name: 'images/[name].[ext]?_[hash:8]'
                }
            }]
        }, {
            test: /\.(woff|woff2|eot|ttf|otf|svg)/,
            use: [{
                loader: 'file-loader',
                options: {
                    name: 'iconfont/[name].[ext]?_[hash:8]'
                }
            }]
        }]
    },

    // 插件
    plugins: plugins
};

module.exports = CommonConfig;